﻿@model KL.Store.Web.Models.ProductInfoModel
@using KL.Store.Core;
@using CaptchaMvc.HtmlHelpers;
@{
    Layout = "~/Views/Shared/_LayoutBasic.cshtml"; ;
    ViewBag.Title = "Edit Properties";
                    //Html.EnableClientValidation(false);
}
<h3>Ürün Özelliklerini Düzenle</h3>
<hr />
<div class="form-group">
    <div class="col-xs-12 first">
        <div id="additionProperties">
            <table class="table table-bordered" id="EditProductInfo">
                @foreach (var info in Model.ProductInfos)
                {
                    <tr data-satirno="@info.SatirNo">
                        <td style="width: 10%;">
                            <input type="text" name="SatirNo" value="@info.SatirNo" data-original="@info.SatirNo" data-satirno="@info.SatirNo" data-field="SATIRNO" data-productid="@Model.ProductId" class="form-control" />
                        </td>
                        <td style="width: 43%;">
                            <input type="text" name="Type" value="@info.Type" data-original="@info.Type" data-satirno="@info.SatirNo" data-field="ACIKLAMA" data-productid="@Model.ProductId" class="form-control" />
                        </td>
                        <td style="width: 43%;">
                            <input type="text" name="Information" value="@info.Information" data-original="@info.Information" data-satirno="@info.SatirNo" data-field="BILGI" data-productid="@Model.ProductId" class="form-control" />
                        </td>
                        <td>
                            <a href="javascript:void(0)" class="RemoveItem" data-satirno="@info.SatirNo"><i class="fa fa-remove fa-2x"></i></a>
                        </td>
                    </tr>
                }
            </table>
            <div class="form-group">
                <div class="col-xs-5 first">
                    <form class="form-inline" role="form">
                        <div class="form-group">
                            <input type="text" name="addType" id="addType" class="form-control" placeholder="İsim" />
                        </div>

                        <div class="form-group">
                            <input type="text" name="addInformation" id="addInformation" class="form-control" placeholder="Değer" />
                        </div>

                        <div class="form-group">
                            <button type="button" id="addSave" class="btn btn-custom1">Ekle</button>
                        </div>
                    </form>
                </div>
                <div class="col-xs-7 first alignright">
                    @if (Model.UstUrunId > 0)
                    {
                        <button type="button" id="UstUrundenGetir" class="btn btn-custom1">Üst Üründen Getir</button>
                    }
                    <button type="button" class="btn btn-custom1" onclick="KapatVeYenile();">Kapat ve Yenile</button>
                </div>
            </div>
            <div id="piResult" style="display: none;"></div>
            <div class="loading"></div>
        </div>
    </div>
</div>
<script>

    $('#EditProductInfo input').live('focusout', function ()
    {
        var column = $(this);
        var row = column.parent().parent();
        var dbfield = column.attr('data-field');
        var newdata = column.val();
        var productid = column.attr('data-productid');
        var satirno = column.attr('data-satirno');
        var original = column.attr('data-original');
        if (original != newdata)
        {
            UpdateExtraField(dbfield, newdata, productid, satirno, original, row, column)
        }
    });

    function UpdateExtraField(dbfield, newdata, productid, satirno, original, row, column)
    {
        loadingMsg();
        $.post('/ProductManage/UrunExtraAlanGuncelle', { Field: dbfield, Value: newdata, ProductId: productid, SatirNo: satirno, Original: original }, function (data)
        {
            $('.loadingMsg').remove();
            if (data == 'success')
            {
                column.attr('data-original', newdata);
                column.attr('value', newdata);
                if (dbfield == 'SATIRNO')
                {
                    row.find('input').attr('data-satirno', newdata);
                }
            }
            else
            {
                column.val(original);
                hataMesaji('Güncelleme işlemi sırasında bir hata oluştu.');
            }
        });
    }

    $('button#kaydetHepsini').click(function ()
    {
        hepsiniKaydet(false);
    });

    $('button#kaydetHepsiniveKapat').click(function ()
    {
        hepsiniKaydet(true);
    });

    function hepsiniKaydet(kapansin)
    {
        loadingMsg();
        var refreshFields = false;
        $('#additionProperties .loading').show();
        var alanlar = $('table#EditProductInfo tr');
        var alanSayisi = alanlar.length;
        //alert(alanSayisi);
        var alanSayaci = 0;
        alanlar.each(function ()
        {
            var OrgSatirNo = $(this).data('satirno');
            var DataSatirNo = $(this).find('td input[name=SatirNo]').val();
            var DataType = $(this).find('td input[name=Type]').val();
            var DataInformation = $(this).find('td input[name=Information]').val();
            var DataProduct = '@(Model.ProductId)';

            $('#piResult').load('/ProductManage/ProductInfoSave', { orgsatirno: OrgSatirNo, datasatirno: DataSatirNo, datatype: DataType, datainformation: DataInformation, dataproduct: DataProduct }, function ()
            {
                alanSayaci++;
                if (alanSayaci == alanSayisi)
                {
                    if (kapansin)
                    {
                        parent.$.fancybox.close();
                        parent.location.reload(true);
                    }
                    else
                    {
                        $('#EditProperties').load('/ProductManage/EditPropertiesGet', { id: '@(Model.ProductId)' }, function ()
                        {
                            $('#additionProperties .loading').hide();
                            $('#processResult').show('fast').html('<div class="bg-info"><p><i class="fa fa-info-circle"></i>&nbsp;Bilgiler Kaydedildi</p></div>').delay(5000).hide();
                            if ($.browser.webkit)
                            {
                                $('body:not(:animated)').animate({ scrollTop: 0 }, 'slow');
                            }
                            else
                            {
                                $('html:not(:animated)').animate({ scrollTop: 0 }, 'slow');
                            }
                        });
                    }
                }

            });
        });
        if (alanSayisi <= 0)
        {
            $('#additionProperties .loading').hide();
            hideMsg();
        }
    }

    $('button#UstUrundenGetir').click(function ()
    {
        if (confirm('Mevcut değerler silinip üst üründeki tüm değerler getirilecektir, emin misiniz?'))
        {
            loadingMsg();
            $('#piResult').load('/ProductManage/ProductPropertiesInsertFromMain', { MainProductId: '@(Model.UstUrunId)', SubProductId: '@(Model.ProductId)' }, function ()
            {
                $('#EditProperties').load('/ProductManage/EditPropertiesGet', { id: '@(Model.ProductId)' }, function ()
                {
                    hideMsg();
                });
            });
        }
    });

    $('.RemoveItem').click(function ()
    {
        if (confirm('Silmek istediğinizden emin misiniz?'))
        {
            loadingMsg();
            var DataSatirNo = $(this).data('satirno');
            var DataProduct = '@(Model.ProductId)';
            //alert(DataType + " " + DataInformation + " " + DataProduct);
            $('#piResult').load('/ProductManage/ProductInfoRemove', { ProductId: DataProduct, SatirNo: DataSatirNo }, function ()
            {
                $('#EditProperties').load('/ProductManage/EditPropertiesGet', { id: '@(Model.ProductId)' }), function ()
                {
                    hideMsg();
                };
                hideMsg();
            });
        }
    });

    //ekle
    $('button#addSave').click(function ()
    {
        loadingMsg();
        var DataType = $('input#addType').val();
        var DataInformation = $('input#addInformation').val();
        var DataProduct = '@(Model.ProductId)';
        $('#piResult').load('/ProductManage/ProductInfoInsert', { ProductId: DataProduct, Type: DataType, Information: DataInformation }, function ()
        {
            $('input#addType').val('');
            $('input#addInformation').val('');
            $('#EditProperties').load('/ProductManage/EditPropertiesGet', { id: '@(Model.ProductId)' }, function ()
            {
                hideMsg();
            });
        });
    });

</script>
